package com.liuzhou.action.subtask;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.liuzhou.dao.SubTaskDao;
import com.liuzhou.dao.impl.SubtaskDaoImpl;
import com.liuzhou.model.Subtask;

public class SubtaskListAction extends HttpServlet{
	private Integer totalPage;//总页数
	private Integer totalRow;//总条数
	private Integer pageRow;//每页显示条数
	private Integer pageNum;//页码
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		pageRow = 8;
	}
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//查询所有子任务列表				
		
		try {
			//获取页面传入的页码（字符串）
			String pageNumStr = req.getParameter("pageNum");
			//用户点击页码
			if( pageNumStr != null ){
				pageNum = Integer.parseInt(pageNumStr);
			}else{
				pageNum = 1;
			}
			
			SubTaskDao subTaskDao = new SubtaskDaoImpl();
			totalRow = subTaskDao.countNum();
			//计算总页数
			totalPage = (totalRow % pageRow) == 0 ? totalRow / pageRow : (totalRow / pageRow) + 1;
			
			//分页查询子任务list
			List<Subtask> list =  subTaskDao.queryAll(pageNum, pageRow);
			
			System.out.println(list);
			
			req.setAttribute("totalRow", totalRow);
			req.setAttribute("totalPage", totalPage);
			req.setAttribute("pageNum", pageNum);
			req.setAttribute("pageRow", pageRow);
			
			req.setAttribute("subtaskList", list);
			req.getRequestDispatcher("/WEB-INF/pages/subtasks/list.jsp").forward(req, resp);
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
		
		
	}
}
